﻿using System;
using System.Collections.Generic;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Interfaces.Services;
using Netkk.Common.Models;
using System.Linq;

namespace Netkk.Library.Services
{
    public class NewsService : INewsService
    {
        private readonly INewsRepository repository;

        /// <summary>
        /// Initializes a new instance of the <see cref="AppointmentService"/> class.
        /// </summary>
        /// <param name="repository">The repository.</param>
        public NewsService(INewsRepository repository)
        {
            this.repository = repository;
        }

        //protected bool ValidateNews(News news)
        //{
        //    if (news.Date == DateTime.MinValue)
        //    {
        //        validationDictionary.AddError("Date", "Bitte eine Datum eingeben.");
        //    }
        //    if (GetNewsById(news.Id) == null)
        //    {
        //        if (news.Date < DateTime.Now.AddDays(-30))
        //        {
        //            validationDictionary.AddError("Date", "Das Datum muss in der Zukunft liegen.");
        //        }
        //    }
        //    if (String.IsNullOrEmpty(news.Title))
        //    {
        //        validationDictionary.AddError("Title", "Bitte einen Titel eingeben.");
        //    }
        //    if (String.IsNullOrEmpty(news.Teaser))
        //    {
        //        validationDictionary.AddError("Teaser", "Bitte eine Kurzbeschreibung eingeben.");
        //    }
        //    if (String.IsNullOrEmpty(news.Text))
        //    {
        //        validationDictionary.AddError("Text", "Bitte einen Text eingeben.");
        //    }
        //    return validationDictionary.IsValid;
        //}

        /// <summary>
        /// Gets the latest news.
        /// </summary>
        /// <returns></returns>
        public News GetLatestNews()
        {
            IEnumerable<News> allnews = repository.Select();
            News news = (from a in allnews
                         where a.Date >= DateTime.Now.AddDays(-30)
                         orderby a.Date descending
                         select a).FirstOrDefault();
            return news;
        }

        /// <summary>
        /// Gets the appointment by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public News GetNewsById(Guid id)
        {
            IEnumerable<News> allnews = repository.Select();
            News news = (from a in allnews
                         where a.Id == id
                         select a).FirstOrDefault();
            return news;
        }

        public News GetNewsByTitle(string title)
        {
            IEnumerable<News> allnews = repository.Select();
            News news = (from a in allnews
                         where EqualsWithCleanUrl(a.Title, title)
                         select a).FirstOrDefault();
            return news;
        }

        private bool EqualsWithCleanUrl(string dirty, string input)
        {
            dirty = Tools.GetCleanUrl(dirty);
            return dirty.Equals(input, StringComparison.OrdinalIgnoreCase);
        }

        public IEnumerable<News> GetNextNews()
        {
            IEnumerable<News> allnews = repository.Select();
            IEnumerable<News> news = from a in allnews
                                     where a.Date >= DateTime.Now.AddDays(-30)
                                     orderby a.Date descending
                                     select a;
            return news;
        }

        public IEnumerable<News> GetPastNews()
        {
            IEnumerable<News> allnews = repository.Select();
            IEnumerable<News> news = from a in allnews
                                     where a.Date < DateTime.Now.AddDays(-30)
                                     orderby a.Date descending
                                     select a;
            return news;
        }

        public void DeleteNews(News news)
        {
            repository.Delete(news);
        }

        public bool SaveNews(News news)
        {
            // Database logic
            try
            {
                repository.Save(news);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}